
var Products = (function(){

	function handleElements(){}

	function handleGrid(){
		var boolRender = function (v, r, i) {
			var color = v == '1' ? 'text-success' : 'text-danger';
			return '<i class="icon-circle ' + color + '"></i>';
		},
			priceRender = function (v) {
				return $.number(v);
			};

		$('#grid').datagrid({
			url: siteURL('products/load_data'),
			title: 'Products List',
			height: 400,
			rownumbers: true,
			pagination: true,
			pagePosition: 'top',
			autoRowHeight: false,
			/*checkbox: true,*/
			singleSelect: true,
			columns: [[
				/*{field: 'ck', checkbox: true},*/
				{
					field: 'p_name',
					title: 'Name',
					width: 100,
					sortable: true
				},
				{
					field: 'cp_name',
					title: 'Category',
					width: 200,
					sortable: true
				},
				{
					field: 'p_created_date',
					title: 'Created Date',
					width: 135,
					align: 'center',
					sortable: true
				},
				{
					field: 'p_modified_date',
					title: 'Modified Date',
					width: 135,
					align: 'center',
					sortable: true
				}
			]]
		});

		var pager = $('#grid').treegrid('getPager');

		pager.pagination({
			buttons: [
				{
					text: '<i class="icon-plus"></i> Add',
					handler: addRecord
				},
				{
					text: '<i class="icon-pencil"></i> Edit',
					handler: editRecord
				},
				{
					text: '<i class="icon-trash"></i> Delete',
					handler: deleteRecord
				},
				'-',
				{
					text: '<i class="icon-picture"></i> View Galery',
					handler: viewGalery
				},
			]
		});
	}

	function handleForm(){
		$('#category').validatebox({
			required: true
		});
	}

	function handleWindows(){
		// win editor
		$('#win').show();
		$('#win').dialog({
			modal: true,
			width: 740,
			title: 'Product Editor',
			buttons: [
				{
					text: 'Submit',
					handler: function () {
						
						$('[name="p_desc"]').val($('#p_desc').code());
						
						$('#form').form('submit', {
							url: siteURL('products/save_data'),
							success: function (response) {
								
								$('#win').dialog('close');
								$('#grid').datagrid('reload');

							}
						});
					}
				},
				{
					text: 'Close',
					handler: function () {
						$('#win').dialog('close');
					}
				}
			]
		});
		$('#win').dialog('close');

		// win galery
		$('#gal').show();
		$('#gal').dialog({
			modal: true,
			width: $('.grid-container .panel.datagrid').width() / 1.3,
			height: 420,
			title: 'Product Galery',
			onOpen: function () {
				$(this).find('.dialog-button a.l-btn').first().show();

			},
			onLoad: function () {
				var uploader = $(this).find('#uploader');
				if (uploader.length) {
					handleUpload();
					$(this).find('.dialog-button a.l-btn').first().hide();
				} else {
					$(this).find('.btn-delete-image').on('click', deleteImage);
				}
			},
			buttons: [
				{
					text: '<i class="icon-picture"></i> Upload Image',
					handler: uploadImage
				},
				{
					text: 'Close',
					handler: function () {
						$('#gal').dialog('close');
					}
				}
			]
		});

		$('#gal').dialog('close');
		$('#tab').tab();
		$('#p_desc').summernote({
			height: 160,
			toolbar: [
				['style', ['style']],
				['font', ['bold', 'italic', 'underline', 'clear']],
				// ['fontname', ['fontname']],
				// ['fontsize', ['fontsize']],  // Still buggy
				['color', ['color']],
				['para', ['ul', 'ol', 'paragraph']],
				['height', ['height']],
				['table', ['table']],
				['insert', ['link', 'picture', 'video']],
				['view', ['fullscreen', 'codeview']],
				// ['help', ['help']]
			]
		});
	}

	function addRecord(){
		$('#win').dialog('open');
		$('#form').form('clear').form('reset');
		$('#p_desc').code('');
	}

	function editRecord(){
		var record = $('#grid').datagrid('getSelected');
		if (!record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}

		$('#win').dialog('open');
		$('#form').form('load', record);

		$('#p_desc').code(record.p_desc);
	}

	function deleteRecord(){
		var record = $('#grid').datagrid('getSelected');
		if (!record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}

		$.messager.confirm('Confirm', 'Delete selected record?', function (r) {
			if (r) {
				$.ajax({
					url: siteURL('products/delete_data'),
					type: 'post',
					dataType: 'json',
					data: record
				})
					.done(function () {
						$('#grid').datagrid('reload');
					});
			}
		});
	}

	function handleSearch(){
		$('#omnisearch').on('keypress', function(e){
			if (e.keyCode === 13) {
				var query = $(this).val();
				$('#grid').datagrid('load', {
					query: query
				});
			}
		});
	}

	/*image*/
	function viewGalery() {
		var record = $('#grid').datagrid('getSelected');
		if (!record) {
			$.messager.alert('Warning', 'No record selected', 'warning');
			return;
		}
		$('#gal').dialog('open');
		$('#gal').dialog('refresh', siteURL('products/load_images/' + record.p_id));
	}

	function uploadImage() {
		$('#gal').dialog('refresh', siteURL('products/product_upload'));
	}

	function handleUpload() {
		var record = $('#grid').datagrid('getSelected');
		$('#fileupload').fileupload({
			url: siteURL('products/product_upload/upload_img/' + record.p_id)
		});
	}

	function deleteImage(e) {
		e.preventDefault();
		var t = $(e.target),
			id = t.data('id'),
			file = t.data('file');
		$.messager.confirm('Confirm', 'Delete image?', function (r) {
			if (r) {
				$.ajax({
					url: siteURL('products/delete_image'),
					type: 'post',
					dataType: 'json',
					data: {
						pi_id: id,
						pi_filename: file
					}
				})
					.done(function () {
						$('#gal').dialog('refresh');
					});
			}
		});
	}

	return {
		init: function(){
			handleElements();
			handleGrid();
			handleWindows();
			handleForm();
			handleSearch();
		}
	};

}(jQuery));

$(document).ready(Products.init);